package org.csu.xzypetstore.controller;

import org.csu.xzypetstore.domain.Account;
import org.csu.xzypetstore.domain.Order;
import org.csu.xzypetstore.service.AccountService;
import org.csu.xzypetstore.service.AdminService;
import org.csu.xzypetstore.service.orderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/AccountManage/")
@SessionAttributes({"userList"})
public class adminAccount {

    @Autowired
    private AdminService adminService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private orderService orderService;

    @GetMapping("allUser")
    public String getAllUsers(Model model)
    {
        List<String> userList = adminService.getAllUsers();
        model.addAttribute("userList",userList);
        return "admin/userAccount";
    }

    @GetMapping("viewUser")
    public String viewUser(String username,Model model)
    {
        List<String> languages = new ArrayList<String>();
        languages.add("Japanese");
        languages.add("English");
        languages.add("Spanish");
        languages.add("Russian");
        languages.add("Chinese");
        model.addAttribute("languages",languages);

        List<String> categories = new ArrayList<>();
        categories.add("FISH");
        categories.add("DOGS");
        categories.add("REPTILES");
        categories.add("CATS");
        categories.add("BIRDS");
        model.addAttribute("categories",categories);

        System.out.println(username);
        Account account = accountService.getAccountByUsername(username);
        model.addAttribute("account",account);
        return "admin/ViewUser";
    }

    @GetMapping("orderList")
    public String orderList(HttpServletRequest request, Model model)
    {
        HttpSession session = request.getSession();
        Account account = (Account)session.getAttribute("account");
        List<Order>orderList = orderService.getOrders();

        model.addAttribute("orderList",orderList);

        return "admin/orderListManage";
    }


    @GetMapping("checkOrder")
    public String checkOrder(HttpServletRequest request,Model model)
    {
        HttpSession session = request.getSession();
        Integer orderId = Integer.valueOf(request.getParameter("orderId"));
        Order order = orderService.getOrder(orderId);
        model.addAttribute("order",order);
        return "admin/viewOrder";
    }

    @GetMapping("deleteOrder")
    public String deleteOrder(HttpServletRequest request,Model model)
    {
        Integer orderId = Integer.valueOf(request.getParameter("orderId"));

        orderService.deleteOrder(orderId);

        List<Order>orderList = orderService.getOrders();

        model.addAttribute("orderList",orderList);

        return "admin/orderListManage";
    }

    @PostMapping("post")
    @ResponseBody
    public String postPublic(String orderId)
    {
        System.out.println(orderId);System.out.println(orderId);System.out.println(orderId);
        Order order = new Order();
        order.setPost("yes");
        System.out.println(orderId);
        int orderid = Integer.parseInt(orderId);
        order.setOrderId(orderid);
        Boolean post = orderService.postProduct(order);

        if(post)
        {
            return "1";
        }
        else
        {
            return "0";
        }

    }
}
